package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.dto.UserLoginDTO;
import com.jumi.microservice.domain.dto.UserTokenDTO;
import com.jumi.microservice.domain.vo.UserLoginVO;
import com.jumi.microservice.domain.vo.UserTokenVO;
import com.jumi.microservice.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 账号登录管理controller组件
 *
 * @author Administrator
 */
@Api(tags = {"后台用户登录管理相关接口"})
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {

    /**
     * 账号登录管理模块的service组件
     */
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 用户登录
     *
     * @param userLogin 用户登录信息vo
     * @return 用户token
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public ResponseResult<UserTokenVO> login(@Validated @RequestBody UserLoginVO userLogin, HttpServletRequest request) {
        UserTokenDTO userToken = userLoginService.login(convertUserVo2Dto(userLogin), request);
        return ResultGenerator.genSuccessResult(convertAccountDto2Vo(userToken));
    }

    /**
     * 用户退出登陆
     *
     * @return 处理结果, 退出登陆成功返回true
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public ResponseResult<Boolean> logout(HttpServletRequest request) {
        String uid = request.getHeader("uid");
        boolean logout = userLoginService.logout(Long.valueOf(uid));
        return ResultGenerator.genSuccessResult(logout);
    }

    /**
     * 用户修改登录密码
     *
     * @return 处理结果, 修改登录密码成功返回true
     */
    @PutMapping("/resetPwd")
    @ApiOperation("重置密码")
    @WebLog(description = "用户修改登录密码")
    public ResponseResult<Boolean> resetPwd(@Validated @RequestParam("password") @NotNull(message = "用户密码不能为空") String password,
                                            HttpServletRequest request) {
        String uid = request.getHeader("uid");
        boolean resetPwd = userLoginService.resetPwd(uid, password);
        return ResultGenerator.genSuccessResult(resetPwd);
    }

    /**
     * 个人中心基本资料修改
     *
     * @param mobile     手机号码
     * @param userAvatar 用户头像
     * @return 处理结果, 修改成功返回true
     */
    @ApiOperation(value = "个人中心基本资料修改", notes = "个人中心基本资料修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true),
            @ApiImplicitParam(name = "userAvatar", value = "用户头像")
    })
    @PutMapping("/updatePersonalBasicData")
    public ResponseResult<Boolean> updatePersonalBasicData(@Validated @RequestParam("mobile") @NotNull(message = "手机号码不能为空") String mobile,
                                                           @RequestParam("userAvatar") String userAvatar) {

        Long uid = getUid();
        boolean updatePersonalBasicData = userLoginService.updatePersonalBasicData(uid, mobile, userAvatar);
        return ResultGenerator.genSuccessResult(updatePersonalBasicData);
    }

    /**
     * 将账号登录VO对象转换为DTO对象
     *
     * @param userLogin 账号VO
     * @return AccountDTO 账号DTO
     */
    private UserLoginDTO convertUserVo2Dto(UserLoginVO userLogin) {
        UserLoginDTO targetAccount = null;
        try {
            targetAccount = userLogin.clone(UserLoginDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetAccount;
    }

    /**
     * 将用户token DTO对象转换为VO对象
     *
     * @param userToken 用户token DTO
     * @return UserTokenVO 用户token vo
     */
    private UserTokenVO convertAccountDto2Vo(UserTokenDTO userToken) {
        UserTokenVO targetAccount = null;
        try {
            targetAccount = userToken.clone(UserTokenVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetAccount;
    }
}
